package lib.bone.access.module.oss;

import lib.bone.access.module.oss.domain.NepuResearchFile;
import lib.bone.common.core.domain.PageResultDTO;
import lib.bone.common.core.domain.ResponseDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Pageable;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author: kindear
 */
@Slf4j
@Service
public class NepuResearchFileServiceImpl implements NepuResearchFileService{
    @Autowired
    NepuResearchFileDAO nepuResearchFileDAO;



    public NepuResearchFileServiceImpl() {
        super();
    }

    @Override
    @Transactional
    public void deleteEntityLink(String entityId) {
        nepuResearchFileDAO.deleteByMapperId(entityId);
    }

    @Override
    public NepuResearchFile saveOne(NepuResearchFile nepuResearchFile) {
        return null;
    }

    @Override
    public NepuResearchFile updateOne(NepuResearchFile nepuResearchFile) {
        return null;
    }

    @Override
    @Async("taskExecutor")
    @Transactional
    public void batchBindFile(String serviceType, String serviceId, List<String> fileIds) {
        log.info("[async]异步写入文件");
        List<NepuResearchFile> files = new ArrayList<>();
        //绑定科研项目和文件
        for (String id:fileIds){
            NepuResearchFile nepuResearchFile = new NepuResearchFile();
            nepuResearchFile.setFileId(id);
            nepuResearchFile.setResearchType(serviceType);
            nepuResearchFile.setMapperId(serviceId);
            files.add(nepuResearchFile);
        }
        nepuResearchFileDAO.saveAll(files);
    }

    @Override
    public List<NepuResearchFile> getList(String researchType, String mapperId) {
        return nepuResearchFileDAO.findByResearchTypeAndMapperId(researchType,mapperId);
    }

    @Override
    public PageResultDTO<NepuResearchFile> getAll(Pageable pageable, Example<NepuResearchFile> example) {
//        return ResponseDTO.wrap(ResponseCodeConst.DEVELOPMENT);
        return null;
    }

    @Override
    public ResponseDTO deleteById(String id) {
        nepuResearchFileDAO.deleteById(id);
        return ResponseDTO.succMsg("删除成功");
    }

    @Override
    public Optional<NepuResearchFile> getById(String id) {

        return nepuResearchFileDAO.findById(id);
    }



    @Override
    public ResponseDTO saveList(List<NepuResearchFile> list) {
        return ResponseDTO.succData(nepuResearchFileDAO.saveAll(list));
    }
}
